﻿using Awsc.DataAcquisitionSys.Core.Common.Extensions;
using Awsc.DataAcquisitionSys.Core.DataAcquisition;
using Awsc.DataAcquisitionSys.Core.Model.Config;
using Awsc.DataAcquisitionSys.Core.Models.ServiceModels;
using Awsc.DataAcquisitionSys.Core.Services.ConfigManagement;
using Microsoft.Extensions.DependencyInjection;

namespace Awsc.DataAcquisitionSys.Core.Services.DataAcquisition.DataCollectors.Sensors
{
    public class CylinderPressSensor:Sensor
    {
        private readonly RegisterConfig config;
        private readonly DeviceTypeEnum deviceType;
        private readonly StringWeightSensorConfig abstractSensorConfig;
        public CylinderPressSensor(AbstractSensorDataCollectionContext abstractSensorDataCollectionContext)
        {
            config = abstractSensorDataCollectionContext.SensorConfig as RegisterConfig;
            deviceType = abstractSensorDataCollectionContext.Service.GetService<IDeviceConfigService>().GetDeviceConfig().DeviceType;
            abstractSensorConfig = config.GetAdditionConfig<StringWeightSensorConfig>();
        }

        protected override void CalculateValue()
        {
            double d = 0;
            var pressPressure = GlobalDeviceManager.Instance.GetSensorValue("下放压力");
            var pressPressureSensor = GlobalDeviceManager.Instance.GetSensor("下放压力");
            var legMode = GlobalDeviceManager.Instance.GetSensorValue("腿模式");
            var legModeAICreated = GlobalDeviceManager.Instance.GetSensorValue("四腿模式Coil");
            if (pressPressure != null)
            {
                double pv = pressPressure.ToDouble() - abstractSensorConfig.NullPress;//下压压力修正值
                double S = abstractSensorConfig.Lift_Cylinder_R / 2000 * abstractSensorConfig.Lift_Cylinder_R / 2000 * Math.PI;//单位平方米
                double s = S - abstractSensorConfig.Lift_Cylinder_Axle_R / 2000 * abstractSensorConfig.Lift_Cylinder_Axle_R / 2000 * Math.PI; //单位 平方米
                if (s > 0)
                {
                    var useLegMode = UseLegMode(legMode, legModeAICreated);
                    double down;
                    if (useLegMode) //4腿模式
                    {
                        down = s * pv * 1000000 * 4 / 9.8;//单位Kg
                    }
                    else //两腿模式
                    {
                        down = s * pv * 1000000 * 2 / 9.8;//单位Kg
                    }
                    switch (deviceType)
                    {
                        case DeviceTypeEnum.川庆170K:
                            d = pv * abstractSensorConfig.PressRadio;
                            break;
                        default:
                            d = down * abstractSensorConfig.PressRadio / 1000;
                            break;
                    }
                }
            }

            if (pressPressureSensor.Status == DevStatus.S_OK)
            {
                this.Status = DevStatus.S_OK;
            }
            else if (pressPressureSensor.Status == DevStatus.S_UNKNOWN)
            {
                this.Status = DevStatus.S_UNKNOWN;

            }
            else
            {
                this.Status = DevStatus.S_ERR;

            }
            this.CurrentValue = d;
            this.OutputValue = d;
        }

        private bool UseLegMode(SensorData legMode, SensorData legModeAI)
        {
            if (legModeAI == null && legMode == null)
            {
                return false;
            }
            if (legModeAI != null && legModeAI.ToBool())
            {
                return true;
            }
            if (legMode != null && legMode.ToBool())
            {
                return true;
            }
            return false;

        }
    }
}
